<?php
namespace Home\Controller\Goods;
use Home\Controller\Layout\LayoutController;
class DetailsController extends LayoutController {
	public function index(){
		// var_dump(I('get.'));
		// $where['tid']=I('get.id');
		// $where['pinpai']=I('get.id');
		$pic = M('g_goods_pic')->select();
		// var_dump($_GET['id']);die;
		if(!empty($_GET['id'])){
			$list = M('g_type')->where('id='.I('get.id'))->find();
			$data = M('g_goods')->where('pinpai='.I('get.id'))->select();
			if(!$data){
				$data = M('g_goods')->where('tid='.I('get.id'))->select();
				if(!$data){
					$arr = explode(' – ',trim($list['type_name'],' €'));
					$where['goods_price'][]=['gt',intval($arr[0])];
					$where['goods_price'][]=['lt',intval($arr[1])];
					$data = M('g_goods')->where($where)->select();
				}
			}
			
		}elseif(!empty($_GET['keyword'])){
			$data = M('g_goods')->where('goods_name='.I('get.keyword'))->select();
		}else{
			echo '<script>window.location.href="'.U('home/goods/details/all').'"</script>';
		}
		foreach($data as $k=>&$v){
			foreach($pic as $key=>$val){
				if($v['id']==$val['gid'] && $val['goods_zpic']==1){
					$v['zpic']=$val['goods_pic'];
				}
			}
		}
		if(empty($data)){
			$list['type_name']='对应的商品不存在';
		}
		$this->type_name=trim($list['type_name'],' €');
		$this->list=$data;
		$this->display();
	}
	public function all(){

		// $this->list=$data;
		$this->display();
	}
	public function data(){
		$data = M('g_goods')->page(I('post.p'),'12')->select();
		$pic = M('g_goods_pic')->select();
		foreach($data as $k=>&$v){
			foreach($pic as $key=>$val){
				if($v['id']==$val['gid'] && $val['goods_zpic']==1){
					$v['zpic']=$val['goods_pic'];
				}
			}
		}
		$this->AjaxReturn($data);
	}
}